{% extends "base.html" %}
{% import "/sideview/macros.html" as sideview %}
{% set query_params=request.query_params %}

{% block title %}完整搜索结果{% endblock title %}
{% block subtitle %}完整搜索结果{% endblock subtitle %}

{% block content %}
<form name="fsearch" onsubmit="return fsearch_submit(this);" method="get">
    <fieldset id="sch_res_detail">
        <legend>搜索详细信息</legend>
        <label class="blind" for="gr_id">选择公告板组</label>
        <select id="gr_id" name="gr_id" class="select">
            <option value="">全分类</option>
            {% for group in groups %}
                <option value="{{ group.gr_id }}" {{ get_selected(query_params.gr_id, group.gr_id) }}>
                    {{ group.gr_subject }}
                </option>
            {% endfor %}
        </select>

        <label class="blind" for="sfl">搜索条件</label>
        <select id="sfl" name="sfl">
            <option value="wr_subject||wr_content" {{ get_selected(query_params.sfl, "wr_subject||wr_content") }})>标题+内容</option>
            <option value="wr_subject" {{ get_selected(query_params.sfl, "wr_subject") }}>标题</option>
            <option value="wr_content" {{ get_selected(query_params.sfl, "wr_content") }}>内容</option>
            <option value="wr_name" {{ get_selected(query_params.sfl, "wr_name") }}>按作者</option>
        </select>

        <label class="blind" for="stx">搜索词<strong class="blind"> 必不可少的</strong></label>
        <span class="sch_wr">
            <input type="text" id="stx" name="stx" class="frm_input" value="{{ query_params.stx }}" size="40" required>
            <button type="submit" class="btn_submit">
                <i class="fa fa-search" aria-hidden="true"></i> 搜索
            </button>
        </span>

        <div class="switch_field">
            <input type="radio" id="sop_and" name="sop" value="and" {{ 'checked' if query_params.sop == "and" or not query_params.sop }}>
            <label for="sop_and">AND</label>
            <input type="radio" id="sop_or" name="sop" value="or" {{ 'checked' if query_params.sop == "or" }}>
            <label for="sop_or">OR</label>
        </div>
    </fieldset>
</form>

<div id="sch_result">
    <section id="sch_res_ov">
        <h2>
            <strong>'{{ query_params.stx }}'</strong> 完整搜索结果
        </h2>
        <ul>
            <li>公告板 {{ boards|length }}狗</li>
            <li>post {{ total_search_count }}枪</li>
        </ul>
    </section>
    {% if total_search_count > 0 %}
        <ul id="sch_res_board">
            <li><a href="{{ request.url.include_query_params(onetable='') }}" {% if not onetable %}class="sch_on"{% endif %}>集成搜索</a></li>
            {% for board in boards %}
                <li>
                    <a href="{{ request.url.include_query_params(onetable=board.bo_table) }}" {% if onetable == board.bo_table %}class="sch_on"{% endif %}>
                        <strong>{{ board.subject }}</strong>
                        <span class="cnt_cmt">{{ board.search_count }}枪</span>
                    </a>
                </li>
            {% endfor %}
        </ul>
        <section class="sch_res_list">
            <div class="search_board_result">
            {% for board in boards if not onetable or onetable == board.bo_table %}
                <h2>
                    <a href="/board/{{ board.bo_table }}?{{ query_params }}">
                        {{ board.subject }} 搜索结果 ({{ board.search_count }})
                    </a>
                    <a href="/board/{{ board.bo_table }}?{{ query_params }}" class="sch_more">更多</a>
                </h2>
                <ul>
                {% for write in board.writes %}
                    <li>
                        <div class="sch_tit">
                            <a href="{{ write.href }}" class="sch_res_title">
                                {% if write.wr_is_comment %}
                                <span class="cmt_def"><i class="fa fa-regular fa-commenting" aria-hidden="true"></i><span class="blind">评论</span></span>
                                {% endif %}
                                {{ write.subject|truncate(100, False, '...', 0)|search_font(query_params.stx)|safe }}
                            </a>
                            <a href="{{ write.href }}" target="_blank" class="pop_a">
                                <i class="fa fa-window-restore" aria-hidden="true"></i>
                                <span class="blind">新窗口</span>
                            </a>
                        </div>
                        <p>{{ write.wr_content|striptags|truncate(100, False, '...', 0)|search_font(query_params.stx)|safe }}</p>
                        <div class="sch_info">
                            {% if board.bo_use_sideview %}
                                {{ sideview.get(request, write.mb_id, write.name, write.email, write.wr_homepage)|safe }}
                            {% else %}
                                <span class="sv_member">{{ write.name }}</span>
                            {% endif %}
                            <span class="sch_datetime"><i class="fa fa-regular fa-clock" aria-hidden="true"></i>
                                {{ write.wr_datetime.strftime("%y-%m-%d %H:%M:%S") }}
                            </span>
                        </div>
                    </li>
                {% endfor %}
                </ul>
            {% endfor %}
            </div>
            {% if onetable and not exists_onetable %}
                <div class="empty_list">未搜索任何结果.</div>
            {% endif %}
        </section>
    {% else %}
        <div class="empty_list">没有搜索到一种材料.</div>
    {% endif %}
</div>
<script>
    function fsearch_submit(f) {
        const stx = f.stx.value.trim();
        if (stx.length < 2) {
            alert("请输入两个以上的字母进行搜索.");
            f.stx.select();
            f.stx.focus();
            return false;
        }

        let cnt = 0;
        for (var i = 0; i < stx.length; i++) {
            if (stx.charAt(i) == ' ')
                cnt++;
        }

        if (cnt > 1) {
            alert("对于快速搜索，您只能在搜索词中输入一个空格.");
            f.stx.select();
            f.stx.focus();
            return false;
        }
        f.stx.value = stx;
        f.action = "";
        return true;
    }
</script>
{% endblock content %}